import logging
logger = logging.getLogger(__name__)

from flask import Blueprint, request, jsonify
from p_harmony.modules.prd.services.prd_cata_dict_service import PrdCataDictService
from p_harmony.database import db_session
from p_harmony.exceptions import APIException
from p_harmony.utils.decorators import api_response

from p_harmony.utils.model_util import ModelUtil
from p_harmony.modules.prd.models.prd_cata_dict import PrdCataDict
from p_harmony.utils.session_util import SessionUtil

prd_cata_dict_blueprint = Blueprint('prd_cata_dict', __name__)

@prd_cata_dict_blueprint.route('/prd_cata_dict', methods=['POST'])
@api_response
def create_prd_cata_dict():
    data = request.get_json()
    model = ModelUtil.convert_request_data_to_model(PrdCataDict, data)
    model.createNo = SessionUtil.get_current_user()["userNo"]
    model.createName = SessionUtil.get_current_user()["userName"]
    service = PrdCataDictService(db_session)
    return service.create(model)

@prd_cata_dict_blueprint.route('/prd_cata_dict/<id>', methods=['GET'])
@api_response
def get_prd_cata_dict(id):
    service = PrdCataDictService(db_session)
    return service.get_by_id(id)

@prd_cata_dict_blueprint.route('/prd_cata_dict/<id>', methods=['PUT'])
@api_response
def update_prd_cata_dict(id):
    data = request.get_json()
    model = ModelUtil.convert_request_data_to_model(PrdCataDict, data)
    service = PrdCataDictService(db_session)
    return service.update(id, model)

@prd_cata_dict_blueprint.route('/prd_cata_dict/<id>', methods=['DELETE'])
@api_response
def delete_prd_cata_dict(id):
    service = PrdCataDictService(db_session)
    return service.delete(id)

@prd_cata_dict_blueprint.route('/prd_cata_dict', methods=['GET'])
@api_response
def get_all_prd_cata_dict():
    page = int(request.args.get('page', 1))
    limit = int(request.args.get('limit', 10))
    service = PrdCataDictService(db_session)
    return service.get_all(page, limit)

@prd_cata_dict_blueprint.route('/prd_cata_dict/page', methods=['GET'])
@api_response
def get_page_prd_cata_dict():
    page = int(request.args.get('page', 1))
    limit = int(request.args.get('limit', 10))
    # 获取查询参数作为过滤条件
    filters = {}
    for key, value in request.args.items():
        if key not in ['page', 'limit']:
            filters[key] = value
    service = PrdCataDictService(db_session)
    return service.get_page(page, limit, filters or None)

